#include <stdio.h>
struct stu
{
  int year;
  char name[20];
  int age;
  char gender[4];
};

int main()
{
  FILE *fp1 = fopen("./student", "w+");
  if (fp1 == NULL)
  {
    printf("student文件打开失败");
  }

  // 定义结构体指针
  struct stu stu1 = {2022, "zxc", 18, "nan"};
  struct stu stu2 = {2022, "zxc", 18, "nan"};
  struct stu stu3 = {2022, "zxc", 18, "nan"};
  struct stu students[3] = {stu1, stu2, stu3};

  // 写入数据
  fwrite(students, sizeof(struct stu), sizeof(students), fp1);
  // 将文件指针放到文件开头,要不然读不到数据
  fseek(fp1, 0, SEEK_SET);

  // 读取数据
  struct stu newstu[3];
  int count = fread(newstu, sizeof(struct stu), 3, fp1);
  if (count == 0)
  {
    printf("没有读到数据");
  }
  // 展示数据
  int i;
  for (i = 0; i < count; i++)
  {
    struct stu temp = newstu[i];
    printf("入学年份:%d\n", temp.year);
    printf("姓名:%s\n", temp.name);
    printf("年龄:%d\n", temp.age);
    printf("性别:%s\n", temp.gender);
  }
  // 关闭文件
  fclose(fp1);

  return 0;
}